package com.thinkgem.jeesite.modules.sys.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.Dict;
import com.thinkgem.jeesite.modules.sys.entity.MailConf;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;

/**
 * 邮箱配置Service
 * @author ThinkGem
 * @version 2017-11-02
 */
@Service
@Transactional(readOnly = true)
public class MailConfService{
	@Autowired
	private DictService dictService;
	
	@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,readOnly = false, rollbackFor = Exception.class)
	public void save(MailConf mailConf) {
		List<Dict> list = DictUtils.getDictList("mail_info");
		for (int i = 0; i < list.size(); i++) {
			Dict dict = list.get(i);
			switch (dict.getLabel()) {
			case "host":
			{
				if (!StringUtils.isEmpty(mailConf.getHost()) && !mailConf.getHost().equals(dict.getValue())) {
					dict.setValue(mailConf.getHost());
					dictService.save(dict);
				}
				break;
			}
			case "user":
			{
				if (!StringUtils.isEmpty(mailConf.getUser()) && !mailConf.getUser().equals(dict.getValue())) {
					dict.setValue(mailConf.getUser());
					dictService.save(dict);
				}
				break;
			}
			case "password":
			{
				if (!StringUtils.isEmpty(mailConf.getPassword()) && !mailConf.getPassword().equals(dict.getValue())) {
					dict.setValue(mailConf.getPassword());
					dictService.save(dict);
				}
				break;
			}
			case "port":
			{
				if (!StringUtils.isEmpty(mailConf.getPort()) && !mailConf.getPort().equals(dict.getValue())) {
					dict.setValue(mailConf.getPort());
					dictService.save(dict);
				}
				break;
			}
			case "from":
			{
				if (!StringUtils.isEmpty(mailConf.getFrom()) && !mailConf.getFrom().equals(dict.getValue())) {
					dict.setValue(mailConf.getFrom());
					dictService.save(dict);
				}
				break;
			}

			default:
				break;
			}
		}
	}
}
